#input thread_id,and view its posts

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup,NavigableString,Tag

from xmw import tianya
import xmw
from xmw.db import ds
from xmw import error
import os
import urllib2, urllib
import uuid
import __main__
from appengine_utilities import sessions
from google.appengine.ext import webapp
import base64
import zlib
import logging
import datetime
import string

class Result:
    def __init__(self):
        self.body=None
        self.title=None
        self.url    =None
           
class MainPage(xmw.RequestHandler):
    
    def tag_to_Str(self,tag):
        text=""
        for c in tag.contents:
            text=text+str(c)
        return text    
    
    def test_url(self,url):
        url=string.strip(url)
        if url.upper().find("CONTENT") <0:
            d=ds.IllegalURL(url=url)
            q=ds.IllegalURL.all()
            q.filter('url = ', url)
            u=q.get()
            if u is None:
                d.put()
            logging.log( logging.INFO, "illegal URl:%s"% url)
            return False
        else:
            return True
                  
    def get(self):
        self.sess = sessions.Session()
        self.cookie_sess = sessions.Session(writer="cookie") 
        
        session_user =None
        if self.sess.has_key("user"):
            session_user=self.sess["user"]
        logging.log( logging.INFO, "session_user :%s"%str(session_user))
        
        template_values ={"session_user":session_user}
        
        if not self.request.GET.has_key('q'):
            self.render_template(template_values, '../html/douban_search.html')
            return
        
        
        q=self.request.GET['q']
        q=q.strip()
        if not self.sess.has_key("user"):
            if self.sess.has_key('login_action'):
                login_action    =   self.sess['login_action']
            else:
                login_action=ds.Login_Action()
                self.sess['login_action']   =login_action
                
            login_action.action ="/search"
            login_action.params="q="+urllib.quote(q.encode('utf-8'))
            login_action.created=datetime.datetime.now()
            login_action.put()
                
        if len(q) == 0:
            self.render_template(template_values, '../html/douban_search.html')
            return
        search_key_word=q
        
        # rewrite the search part with google search ajax 2010 1 23
        
        
        q=q+" site:www.tianya.cn"
        q=urllib.quote(q.encode("GB18030"))


        url = 'http://www.baidu.com/s?wd=%s'%q
        #request = urllib2.Request(url, None, {'Referer': 'www.tianya.cn'})
        logging.log( logging.INFO, "url: %s"% (url))
        page = urllib2.urlopen(url)
        #page = urllib2.urlopen(url)
        soup = BeautifulSoup(page, fromEncoding="GB18030")
        #soup = BeautifulSoup(page)
        page.close()
 
        lst_result=[]
        try:
            for i in range(1,11):
                t=soup.find('table',{"id":str(i)})
                c=t.tr.td
                cc=c.contents
            
                title=cc[0]
                
                url=title["href"]
                title=self.tag_to_Str(title)
                body=cc[2]
                a=body.find('a')
                a.extract()

                text=self.tag_to_Str(body)
                #body.insert(-1,tag)

                result=Result()
                result.body=text
  
                result.url=url
                if not self.test_url(url):
                    continue
                    
                result.title    =title
                #result.q_title=urllib.quote(title)
                
                result.q_title=self.xmw_encoding(title)
                result.q_url=self.xmw_encoding(url)
                #result.q_title=title

                lst_result.append(result)
        except:
            import traceback
            traceback.print_exc()
            lst_result=[]
            lst_result.append("sorry,fail to find posts!")
            
        search_key_word=search_key_word.encode('utf-8')
        q_search_key_word=self.xmw_encoding(urllib.quote(search_key_word))
        template_values2 = {
                           "lst_result":lst_result,
                           "search_key_word":search_key_word,
                           "q_search_key_word":q_search_key_word,
                           "post_count":len(lst_result),
                           #"login_action":self.xmw_encoding("/search?q=%s"%search_key_word),
                           
                           }  
        template_values.update(template_values2)  
        
        self.render_template(template_values, '../html/douban_search.html')
                        
        #self.render_template(None, "../html/douban_search")
        return

def main():
    application = webapp.WSGIApplication(
                                       [('/search', MainPage)],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
